package com.liehacker.android;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

import com.liehacker.android.etcs.ActivityControlActivity;
import com.liehacker.android.etcs.GetConfigActivity;
import com.liehacker.android.etcs.GetIPfromDns;
import com.liehacker.android.etcs.MyIntentActivity;
import com.liehacker.android.etcs.OtherAppExecute;
import com.liehacker.android.etcs.SecurityActivity;
import com.liehacker.android.layout.C03_EditText;
import com.liehacker.android.layout.C03_TextView;
import com.liehacker.android.layout.C03_VideoView;
import com.liehacker.android.layout.C04_Relative;
import com.liehacker.android.layout.C04_Relative_Animation;
import com.liehacker.android.mapservice.C24_63Search;
import com.liehacker.android.mapservice.C24_ChangeFormat;
import com.liehacker.android.mapservice.C24_FishAlarm;
import com.liehacker.android.mapservice.C24_GeoCoding;
import com.liehacker.android.mapservice.C24_GetProvider;
import com.liehacker.android.mapservice.C24_LastKnown;
import com.liehacker.android.mapservice.C24_MapView;
import com.liehacker.android.mapservice.C24_OverlayLocation;
import com.liehacker.android.mapservice.C24_OverlayMulti;
import com.liehacker.android.mapservice.C24_OverlayWidget;
import com.liehacker.android.mapservice.C24_ReadLocation;
import com.liehacker.android.ontop.LiehackerSuperActivity;
import com.liehacker.android.util.DebugLog;

public class LiehackerSampleActivity extends LiehackerSuperActivity {

	String[] arChapter = {
			"3장 레이아웃",
			"4장 레이아웃 관리",
			"24장 맵 서비스",		
			"Etc(여러잡동사니)...",
			"Etc(Network)"
	};
	
	Spinner mSpinner; 
	ListView mExamList;
	ArrayAdapter<CharSequence>	mAdapter;
	boolean mInitSelection = true;
	boolean mOmitChapter;
	int mFontSize;
	int mBackType;
	boolean mDescSide; 

	
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        mExamList = (ListView)findViewById(R.id.examlist);
        mSpinner = (Spinner)findViewById(R.id.spinnerchapter);
        mSpinner.setPrompt("장을 선택하삼.."); 
        
        mAdapter = new ArrayAdapter<CharSequence>(this,android.R.layout.simple_spinner_item,arChapter);
        mAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
        mSpinner.setAdapter(mAdapter); 
              
        DebugLog.LOGLEVEL = android.util.Log.ASSERT - 1;
        
        mSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int position, long id) { 	 
				
				if(mInitSelection) { 
					mInitSelection = false;
					SharedPreferences pref = getSharedPreferences("LiehackerExam",0);
					int lastchapter = pref.getInt("LastChapter", 0);
					mSpinner.setSelection(lastchapter);
					ChangeChapter(lastchapter);
				}else {
					ChangeChapter(position);
					SharedPreferences pref = getSharedPreferences("LiehackerExam",0);
					SharedPreferences.Editor edit = pref.edit();
					edit.putInt("LastChapter", position);
					edit.commit(); 
				}
				
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
			}
        });
        
        ReadOption();
        
        boolean bRunLast = false;
        
        if(bRunLast) {
        	SharedPreferences pref = getSharedPreferences("LiehackerExam",0);
        	int pkg = pref.getInt("LastChapter", 0);
        	int pos = pref.getInt("LastPosition", 0);
        	ChangeChapter(pkg);
        	Intent intent = new Intent(this, arExample.get(pos).cls);
        	startActivity(intent);
        }
        
        
    }

	private void ReadOption() {
		// TODO Auto-generated method stub
		SharedPreferences pref = getSharedPreferences("LiehackerExam",0);
		mFontSize = pref.getInt("mFontSize", 1);
		mBackType = pref.getInt("mBackType", 0);
		mDescSide = pref.getBoolean("mDescSide", false);
		mOmitChapter = pref.getBoolean("mOmitChapter", false);
		
		if( mBackType != 0 ) {
			mExamList.setBackgroundColor(0xff101010);
			mExamList.setDivider(new ColorDrawable(0xff606060));
			mExamList.setDividerHeight(1);
			
		} else {
			mExamList.setBackgroundColor(0xffe0e0e0);
			mExamList.setDivider(new ColorDrawable(0xff404040));
			mExamList.setDividerHeight(1);
		}
	}

	protected void ChangeChapter(int lastchapter) {
		// TODO Auto-generated method stub
		FillExample(lastchapter);
		ExamListAdapter Adapter = new ExamListAdapter(this);
		mExamList.setAdapter(Adapter);
		
		final Context ctx = this;
		mExamList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				SharedPreferences pref = getSharedPreferences("LiehackerExam",0);
				SharedPreferences.Editor edit = pref.edit();
				
				edit.putInt("LastPosition", arg2);
				edit.commit();
				Intent intent = new Intent(ctx, arExample.get(arg2).cls);
				startActivity(intent);				
			}
			
		});
		
	}

	ArrayList<Example> arExample = new ArrayList<Example>();
	private void FillExample(int lastchapter) {
		// TODO Auto-generated method stub
		arExample.clear();
		
		switch(lastchapter) {
		case 0 :
			arExample.add(new Example(C03_TextView.class, "택스트 뷰 위젯 소개"));
			arExample.add(new Example(C03_EditText.class, "EditText KeyEvent"));
			arExample.add(new Example(C03_VideoView.class, "VideoView Player"));
			break;
		case 1:
			arExample.add(new Example(C04_Relative.class, "렐레티브 레이아웃의 상대적 배치"));
			arExample.add(new Example(C04_Relative_Animation.class, "탑 바 에니메이션"));
			break;
		case 2:
			arExample.add(new Example(C24_GetProvider.class, "위치 제공자 조사."));
			arExample.add(new Example(C24_ReadLocation.class, "현재 위치 읽기"));
			arExample.add(new Example(C24_LastKnown.class, "최근 위치 조사"));
			arExample.add(new Example(C24_ChangeFormat.class, "포멧 변환"));
			arExample.add(new Example(C24_63Search.class, "63빌딩 찾기"));
			arExample.add(new Example(C24_MapView.class,"Google Api 맵뷰 테스트"));
			arExample.add(new Example(C24_GeoCoding.class, "Address Change"));
			arExample.add(new Example(C24_FishAlarm.class,"도착 알림"));
			arExample.add(new Example(C24_OverlayLocation.class,"현재 위침 및 나침판"));
			arExample.add(new Example(C24_OverlayMulti.class, "오버레이 항목"));
			arExample.add(new Example(C24_OverlayWidget.class, "오베레이 위젝"));
			break;
		case 3: 
			arExample.add(new Example(GetConfigActivity.class,"코드 테스트용 엑티비티"));
			arExample.add(new Example(ActivityControlActivity.class,"부모 엑티비티 제어"));
			arExample.add(new Example(OtherAppExecute.class, "다른 앱 실행"));
			arExample.add(new Example(SecurityActivity.class, "Des 암호화"));
			arExample.add(new Example(MyIntentActivity.class, "Intent (전화, )" ));
			break;
		case 4:
			arExample.add(new Example(GetIPfromDns.class, "DNS로 IP알아오기"));
			break;
		}
		
	}
	
	class Example { 
		Example(Class<?> acls, String aDesc) { 
			cls = acls;
			Name = cls.getSimpleName();
			
			if(mOmitChapter) {
				int underbar = Name.indexOf('_');
				if(underbar != -1){
					Name = Name.substring(underbar + 1);
				}
			}
			Desc = aDesc;
		}
		
		Class<?> cls;
		String Name;
		String Desc;
	}
	
	class ExamListAdapter extends BaseAdapter {
		Context maincontext;
		LayoutInflater Inflater;
		
		public ExamListAdapter(Context context) {
			maincontext = context;
			Inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return arExample.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return arExample.get(position).Name;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			
			if(convertView == null) {
				convertView = Inflater.inflate(R.layout.andexamlist, parent, false);
			}
			
			LinearLayout examlayout = (LinearLayout)convertView.findViewById(R.id.examlist);
			TextView txt1 = (TextView)convertView.findViewById(R.id.text1);
			TextView txt2 = (TextView)convertView.findViewById(R.id.text2);
			
			if(mDescSide) {
				examlayout.setOrientation(LinearLayout.HORIZONTAL);
			}
			
			if(mBackType != 0) {
				examlayout.setBackgroundColor(Color.BLUE);
				txt1.setTextColor(Color.WHITE);
				txt1.setTextColor(Color.LTGRAY);
			}
			
			switch(mFontSize) {
			case 0:
				txt1.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13);
				txt2.setTextSize(TypedValue.COMPLEX_UNIT_SP,  9);
				break;
			case 1:
				txt1.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
				txt2.setTextSize(TypedValue.COMPLEX_UNIT_SP, 11);
				break;
			}
		
			txt1.setText(arExample.get(position).Name);
			txt2.setText(arExample.get(position).Desc);
			
			return convertView;
		}
		
		
	}
	
}